package com.yycx.module.bus.provider.handler.process.listener;

import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.service.BusOrderDetailsService;
import com.yycx.module.bus.provider.service.BusPaymentService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 厂家返款审批结束监听器
 *
 * @author zyf
 */
@Component
public class RefundProcessEndListener implements ExecutionListener {

    @Autowired
    private BusPaymentService paymentService;


    @Autowired
    private BusOrderDetailsService orderDetailsService;


    @Override
    public void notify(DelegateExecution delegateExecution) {
        String businessKey = delegateExecution.getProcessInstanceBusinessKey();
        BusPayment payment = paymentService.getById(businessKey);
        String amountType = payment.getAmountType();
        //厂家返款
        if (AmountTypeEnum.Refund.getCode().equals(amountType)) {
            //开票税金ID
            Long kaipiaoId = payment.getParentId();
            payment.setPaymentState(CommonConstants.INT_2);
            //更新返款状态为已返款
            orderDetailsService.updateFankuanInfo(payment, kaipiaoId);
        }
        //厂家退垫付款
        if (AmountTypeEnum.SbReturn.getCode().equals(amountType)) {
            orderDetailsService.updateReturnDianfuAmount(payment);
        }
        //更新为已审批状态
        paymentService.updatePaymentState(payment.getPaymentId(), CommonConstants.INT_2);
    }
}
